package com.djf.dto;

import com.djf.entity.Order;
import com.djf.entity.User;
import com.djf.entity.UserDelivery;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单-用户-配送关联查询DTO
 * 用于返回三表关联查询的结果
 *
 * @author djf
 * @since 2025-01-01
 */
@Data
@Accessors(chain = true)
public class OrderUserDeliveryDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    // ========== 订单信息 ==========
    /**
     * 订单ID
     */
    private Long orderId;

    /**
     * 订单编号
     */
    private String orderNo;

    /**
     * 订单金额
     */
    private BigDecimal orderAmount;

    /**
     * 订单状态
     */
    private Integer orderStatus;

    /**
     * 订单创建时间
     */
    private LocalDateTime orderCreateTime;

    // ========== 用户信息 ==========
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户姓名
     */
    private String userName;

    /**
     * 用户手机号
     */
    private String userPhone;

    /**
     * 用户邮箱
     */
    private String userEmail;

    /**
     * 用户性别
     */
    private Integer userGender;

    /**
     * 用户状态
     */
    private Integer userStatus;

    // ========== 配送信息 ==========
    /**
     * 配送记录ID
     */
    private Long deliveryId;

    /**
     * 配送单号
     */
    private String deliveryNo;

    /**
     * 收货人姓名
     */
    private String receiverName;

    /**
     * 收货人电话
     */
    private String receiverPhone;

    /**
     * 配送地址
     */
    private String deliveryAddress;

    /**
     * 省份
     */
    private String province;

    /**
     * 城市
     */
    private String city;

    /**
     * 区县
     */
    private String district;

    /**
     * 配送状态
     */
    private Integer deliveryStatus;

    /**
     * 快递员姓名
     */
    private String courierName;

    /**
     * 快递员电话
     */
    private String courierPhone;

    /**
     * 配送公司
     */
    private String deliveryCompany;

    /**
     * 快递单号
     */
    private String trackingNumber;

    /**
     * 预计送达时间
     */
    private LocalDateTime estimatedTime;

    /**
     * 实际送达时间
     */
    private LocalDateTime actualTime;

    /**
     * 配送创建时间
     */
    private LocalDateTime deliveryCreateTime;

    /**
     * 租户ID
     */
    private Long tenantId;

    /**
     * 从实体对象构建DTO
     *
     * @param order    订单信息
     * @param user     用户信息
     * @param delivery 配送信息
     * @return DTO对象
     */
    public static OrderUserDeliveryDTO build(Order order, User user, UserDelivery delivery) {
        OrderUserDeliveryDTO dto = new OrderUserDeliveryDTO();
        
        // 订单信息
        if (order != null) {
            dto.setOrderId(order.getId())
               .setOrderNo(order.getOrderNo())
               .setOrderAmount(order.getTotalAmount())
               .setOrderStatus(order.getOrderStatus())
               .setOrderCreateTime(order.getCreateTime())
               .setUserId(order.getUserId())
               .setTenantId(order.getTenantId());
        }
        
        // 用户信息
        if (user != null) {
            dto.setUserName(user.getUserName())
               .setUserPhone(user.getPhone())
               .setUserEmail(user.getEmail())
               .setUserGender(user.getGender())
               .setUserStatus(user.getStatus());
        }
        
        // 配送信息
        if (delivery != null) {
            dto.setDeliveryId(delivery.getId())
               .setDeliveryNo(delivery.getDeliveryNo())
               .setReceiverName(delivery.getReceiverName())
               .setReceiverPhone(delivery.getReceiverPhone())
               .setDeliveryAddress(delivery.getDeliveryAddress())
               .setProvince(delivery.getProvince())
               .setCity(delivery.getCity())
               .setDistrict(delivery.getDistrict())
               .setDeliveryStatus(delivery.getDeliveryStatus())
               .setCourierName(delivery.getCourierName())
               .setCourierPhone(delivery.getCourierPhone())
               .setDeliveryCompany(delivery.getDeliveryCompany())
               .setTrackingNumber(delivery.getTrackingNumber())
               .setEstimatedTime(delivery.getEstimatedTime())
               .setActualTime(delivery.getActualTime())
               .setDeliveryCreateTime(delivery.getCreateTime());
        }
        
        return dto;
    }

    /**
     * 获取订单状态描述
     */
    public String getOrderStatusDesc() {
        if (orderStatus == null) return "未知";
        switch (orderStatus) {
            case 1: return "待支付";
            case 2: return "已支付";
            case 3: return "已发货";
            case 4: return "已完成";
            case 5: return "已取消";
            default: return "未知";
        }
    }

    /**
     * 获取配送状态描述
     */
    public String getDeliveryStatusDesc() {
        if (deliveryStatus == null) return "未知";
        switch (deliveryStatus) {
            case 1: return "待配送";
            case 2: return "配送中";
            case 3: return "已送达";
            case 4: return "配送失败";
            default: return "未知";
        }
    }

    /**
     * 获取用户性别描述
     */
    public String getUserGenderDesc() {
        if (userGender == null) return "未知";
        switch (userGender) {
            case 1: return "男";
            case 2: return "女";
            default: return "未知";
        }
    }
}